/*
 * =====================================================================================
 *
 *       Filename:  getopt.c
 *
 *    Description:  getopt getopt_long getopt_long_only
 *
 *        Version:  1.0
 *        Created:  2013年01月10日 14时07分24秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  jianxi sun (jianxi), ycsunjane@gmail.com
 *   Organization:  
 *
 * =====================================================================================
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>


int main(int argc,char **argv)
{
	int result;
	/* 使getopt 不向stderr 输出错误信息 */
	opterr = 0;
	printf("argc:%d\n", argc);
	while((result=getopt(argc,argv,"-ab:c::")) != -1)
	{
		switch(result)
		{
			case 'a':
				printf("option=a,optopt=%c,optarg=%s,optind=%d\n",optopt,optarg,optind);
				break;
			case 'b':
				printf("option=b,optopt=%c,optarg=%s,optind=%d\n",optopt,optarg,optind);
				break;
			case 'c':
				printf("option=c,optopt=%c,optarg=%s,optind=%d\n",optopt,optarg,optind);
				break;
			case '?':
				printf("result=?,optopt=%c,optarg=%s,optind=%d\n",optopt,optarg,optind);
				break;
			case -1:
				printf("error\n");
				break;
			default:
				printf("result is %d \n",result);
				exit(-1);
		}
		printf("argv[%d]=%s\n",optind,argv[optind]);
	}
	printf("result=-1, optind=%d\n", optind);   //看看最后optind的位置
	for(result = optind; result < argc; result++)
		printf("-----argv[%d]=%s\n", result, argv[result]);

	//看看最后的命令行参数，看顺序是否改变了哈。
	for(result = 1; result < argc; result++)
		printf("\nat the end-----argv[%d]=%s\n", result, argv[result]);
	return 0;
}
